# -*- coding: utf-8 -*-
"""
 @Time    : 2021/2/4 下午6:07
 @Author  : tgy
 
"""
from urllib.request import urlopen
import io


class UrlTemplate:

    def __init__(self, template):
        self.template = template

    def request(self, **kwargs):
        ...
        response = urlopen(self.template.format_map(kwargs))
        decode_response = io.TextIOWrapper(buffer=response, encoding='utf-8')
        for line in decode_response:
            yield line


def url_template(template):
    ...

    def request(**kwargs):
        ...
        response = urlopen(template.format_map(kwargs))
        for line in io.TextIOWrapper(response, encoding='utf-8'):
            yield line

    return request


def test01():
    ...
    # templates = UrlTemplate('http://www.baidu.com?name={name}&kw={kw}')
    template = url_template('http://www.baidu.com?name={name}&kw={kw}')
    params = {'kw': 'zhangsan', 'name': 'tony'}
    for line in template(**params):
        print(line)


if __name__ == '__main__':
    ...
    test01()